package com.dlc.shop.seckill.common.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.bean.event.SubmitSeckillOrderEvent;
import com.dlc.shop.bean.model.Order;
import com.dlc.shop.bean.model.OrderItem;
import com.dlc.shop.seckill.common.model.Seckill;
import com.dlc.shop.seckill.common.service.SeckillService;
import com.dlc.shop.service.OrderItemService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 获取订单相关信息
 * @author yami
 */
@Component("seckillGetSeckillOrderInofListener")
@AllArgsConstructor
public class GetSeckillOrderInofListener {

    private final OrderItemService orderItemService;
    private final SeckillService seckillService;

    @EventListener(SubmitSeckillOrderEvent.class)
    public void seckillGetSeckillOrderInofListener(SubmitSeckillOrderEvent event) {
        Order order = event.getOrder();
        event.setMaxCancelTime(0);
        if (Objects.isNull(order)) {
            return;
        }
        // TODO stock
        // 秒杀只有一个订单项
        OrderItem orderItem = orderItemService.getOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderNumber, order.getOrderNumber()));
        if (Objects.isNull(orderItem)) {
            return;
        }
        Seckill seckill = seckillService.getSeckillById(orderItem.getActivityId());
        if (Objects.isNull(seckill)) {
            return;
        }
        event.setMaxCancelTime(seckill.getMaxCancelTime());
    }

}
